#TODO: dependencies...

class Level(CompositeBody):
    """Contains all of the content of the game level
       
       Dependencies: xml.dom.minidom
    """
    def __init__(self, engine, name="level"):
        CompositeBody.__init__(self, engine, name)
        self.path = os.path.join(os.getcwd(), "Levels")
    
    def load(self, name):
        file = open(os.path.join(self.path, name + '.xml') )
        xmlDoc = xmlHandler.parse(file)
        xmlOuden = xmlDoc.getElementsByTagName("ouden")[0]
        xmlLevel = xmlOuden.getElementsByTagName("level")[0]
        for xmlGeometry in xmlLevel.getElementsByTagName("solid_geometry"):
            collideChars = xmlGeometry.getAttribute("collideChars")
            collideBullets = xmlGeometry.getAttribute("collideBullets")
            friction = float(xmlGeometry.getAttribute("friction"))
            restitution = float(xmlGeometry.getAttribute("restitution"))
            for xmlPolygon in xmlGeometry.getElementsByTagName("polygon"):
                isClosed = xmlPolygon.getAttribute("closed")
                shapeDef = b2PolygonDef()
                shapeDef.friction = friction
                shapeDef.restitution = restitution
                vertices = []
                for xmlVertex in xmlPolygon.getElementsByTagName("vertex"):
                    vecString = xmlVertex.getAttribute("pos")
                    pos = tuple( [float(x) for x in (vecString.split(","))] )
                    vertices.append(pos)
                shapeDef.setVertices_tuple(tuple(vertices))
                self.engine.physics_world.GetGroundBody().CreateShape(shapeDef)
            for xmlCircle in xmlGeometry.getElementsByTagName("circle"):
                shapeDef = b2CircleDef()
                shapeDef.friction = friction
                shapeDef.restitution = restitution
                vecString = xmlCircle.getAttribute("pos")
                pos = tuple( [float(x) for x in (vecString.split(","))] )
                shapeDef.localPosition = b2Vec2(pos[0], pos[1])
                shapeDef.radius = float(xmlCircle.getAttribute("radius"))
                self.engine.physics_world.GetGroundBody().CreateShape(shapeDef)
        for xmlSensor in xmlLevel.getElementsByTagName("sensor"):
            pass
        for xmlModel in xmlLevel.getElementsByTagName("model"):
            name = xmlModel.getAttribute("name")
            texture_name = xmlModel.getAttribute("texture")
            texture = self.engine.renderer.textureLoader.getTexture(texture_name)
            layer_name = xmlModel.getAttribute("layer")
            xmlMesh = xmlModel.getElementsByTagName("mesh")[0]
            mesh_name, mesh = self.engine.renderer.meshLoader._loadMesh(xmlMesh)
            self.models[name] = self.engine.renderer.\
                                createModel(layer=layer_name, \
                                            mesh=mesh, \
                                            texture=texture, \
                                            local_xform=b2XForm_identity)
        for xmlEntity in xmlLevel.getElementsByTagName("entity"):
            pass
